import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout/Layout'
import { v4 } from 'uuid'

Vue.use(Router)
export const constantRoutes = addRouteUuid([
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/Redirect'),
        meta: { menuId: '-3' }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/Register'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home'),
        name: 'Home',
        // 不要修改 meta, 因为别的地方也在用，由于是首页，写死即可，搜索 'menuId: '-1''
        meta: { title: '首页', icon: 'menu-home', affix: true, menuId: '-1' }
      }
    ]
  }
])
// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = addRouteUuid([
  {
    path: '/system/post-auth',
    query: {},
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'post',
        component: () => import('@/views/system/role/AuthPost'),
        name: 'AuthPost',
        meta: { title: '分配岗位', activeMenu: '/organ/role' }
      }
    ]
  },
  {
    path: '/system/user-auth',
    component: Layout,
    permissions: ['system:post:edit'],
    hidden: true,
    children: [
      {
        path: 'user/:postId(\\d+)',
        component: () => import('@/views/system/post/AuthUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/organ/post' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    query: {},
    component: Layout,
    permissions: ['system:menu:edit'],
    hidden: true,
    children: [
      {
        path: 'role',
        component: () => import('@/views/system/menu/AuthRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/organ/menu' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',

        component: () => import('@/views/system/dict/Data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' }
      }
    ]
  }
])

export function addRouteUuid(routes) {
  routes.forEach(item => {
    if (item.children) {
      addRouteUuid(item.children)
    }
    if (!item.meta) {
      item.meta = {
        menuId: v4()
      }
    } else if (!item.meta.menuId) {
      item.meta.menuId = v4()
    }
  })
  return routes
}

const router = new Router({
  base: process.env.VUE_APP_BASE_URL,
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

export default router
